package com.gsm.controller;

import com.gsm.dto.LoginUserDto;
import com.gsm.entity.RespCode;
import com.gsm.entity.Result;
import com.gsm.service.LoginService;
import com.gsm.service.SmsService;
import com.gsm.vo.LoginUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("login")
public class LoginController {
    @Autowired
    private LoginService loginService;
    @Autowired
    private SmsService smsService;

    /**
     * 账号密码登录
     *
     * @param dto
     * @return
     */
    @PostMapping("user")
    public Result selectUserInfo(@Valid @RequestBody LoginUserDto dto) {
        LoginUserVo vo = loginService.selectUserInfo(dto);
        return Result.success(vo);
    }

    /**
     * 验证码登录
     *
     * @return
     */
    @GetMapping("user/{iphoneNo}/{code}")
    public Result selectUserInfoByCode(@PathVariable("iphoneNo") String iphoneNo, @PathVariable("code") String code) {
        boolean flag = smsService.selectIphoneNoAndCode(iphoneNo,code);
        if(!flag){
            return Result.error(RespCode.LOGIN_VERIFY_CODE_DENY);
        }
        LoginUserVo vo = loginService.selectUserInfoByIphoneNo(iphoneNo);
        return Result.success(vo);
    }

    /**
     * 发送短信验证码
     *
     * @return
     */
    @GetMapping("user/{iphoneNo}")
    public Result sendSmsCode(@PathVariable("iphoneNo") String iphoneNo) {
        boolean flag = loginService.selectIphoneNoIsExist(iphoneNo);
        if (!flag) {
            return Result.error(RespCode.REG_NAME_NOT_EXIST_ERROR);
        }
        //发送短信
        smsService.sendSms(iphoneNo);
        return Result.success();
    }
}
